package com.songlanyun.modules.material.controller.api;

import com.songlanyun.common.enums.NoticeConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.material.entity.SystemNoticeEntity;
import com.songlanyun.modules.material.service.SystemNoticeService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 平台通知公告
 *
 * @author zx
 * @date 2021年3月1日 11:42:07
 */
@RestController
@RequestMapping("api/notice")
public class SystemNoticeApiController extends AbstractController {
    @Autowired
    private SystemNoticeService systemNoticeService;


    /**
     * 分页列表
     */
    @GetMapping("/page")
    public R page(@RequestParam Map<String, Object> params) {
        params.put("state", NoticeConstant.StateEnum.RELEASE.getCode());
        PageUtils page = systemNoticeService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        return R.ok().put("data", systemNoticeService.getById(id));
    }

    /**
     * 获取最新公告
     */
    @GetMapping("/newest")
    public R newest() {
        SystemNoticeEntity one = systemNoticeService.lambdaQuery()
                .eq(SystemNoticeEntity::getState, NoticeConstant.StateEnum.RELEASE)
                .orderByDesc(SystemNoticeEntity::getNoticeTime)
                .last("limit 1")
                .one();
        return R.ok().put("data", one);
    }
}
